# -*- coding: utf-8 -*-

# dialogs/scan.py

# Copyright 2009-2010 Diego Hansen Hahn (aka DiegoHH) <diegohh90 [at] hotmail [dot] com>

# lazynds is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License.

# lazynds is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with lazynds. If not, see <http://www.gnu.org/licenses/>.

import os
import time

import gtk

import pluginmanager

class Scan:

    def __init__(self, builder, parent, bios, complist, plugins):
        self.builder = builder
        self.bios = bios

        self.dialog = self.builder.get_object('scan_dialog')
        self.dialog.set_transient_for(parent)

        self.progbar = self.builder.get_object('progbar')

        self.complist = complist
        self.plugins = plugins

    def run(self):
        plugin_results = []
        bios_results = []

        self.progbar.set_text('Aguardando...')
        self.progbar.set_fraction(0.0)

        while True:
            if self.dialog.run() == gtk.RESPONSE_OK:
                self.dialog.set_sensitive(False)

                self.bios.decode_list = self.complist.active_list
                bios_results += self.bios.scan(self.progbar)

                if self.plugins.active_list:
                    for x in self.plugins.active_list:
                        plugin_results += pluginmanager.trigger_event(x+'-SCAN',
                                                              file=self.bios.fd,
                                                              progbar=self.progbar)
                time.sleep(0.5)

                self.dialog.hide()
                self.dialog.set_sensitive(True)
                return bios_results, plugin_results
            else:

                self.dialog.hide()
                self.dialog.set_sensitive(True)
                return None, None

